
import { user_listAll } from '@/api/user'
import { group_findUsersById } from '@/api/group'

let data=[] 
function transTreeData(items) {
  if (items.length > 0) {
    let curPid = 0 //pid=0,为最上层节点 ,即无父节点 
    let parent = findChild(curPid); //数组 
    return parent;
  } else {
    return [];
  }
}
//找子节点 
function findChild(curPid) {
  let _arr = [];
  let items = data;
  let length = items.length;
  for (let i = 0; i < length; i++) {
    if (items[i].parent_id == curPid) {
      let _obj = items[i];
      _obj.label = items[i].dept_name;
      _obj.children = findChild(_obj.id);
      _arr.push(_obj);
    }
  }
  return _arr;
}
export function formateTree(e) {//格式化成树结构
    data = e;
    return transTreeData(e);
}


let depts=[] 
function transGroupTreeData(items) {
  if (items.length > 0) {
    let curPid = 0 //pid=0,为最上层节点 ,即无父节点 
    let parent = findGroupChild(curPid); //数组 
    return parent;
  } else {
    return [];
  }
}
//找子节点 
function findGroupChild(curPid) {
  let _arr = [];
  let items = depts;
  let length = items.length;
  for (let i = 0; i < length; i++) {
    if (items[i].parent_id == curPid) {
      let _obj = items[i];
      _obj.label = items[i].dept_name;
      _obj.must = false ;
      _obj.children = findGroupChild(_obj.id);
      user_listAll({dept_id: _obj.id, company_id: localStorage.getItem("companyId")}).then(res => {
        let formateUser = fomateUser(res.data.data.rows);
        _obj.children = _obj.children.concat(formateUser);
        _obj.num = formateUser.length //人数
        _arr.push(_obj);
      });
    }
  }
  return _arr;
}
function fomateUser(e){
  let user_arr = []
  for (let i = 0; i < e.length; i++) {
    let user = e[i];
    let args = { label: user.user_name, id: "user_id" + user.id, must: true }
    let result = Object.assign({}, user, args)
    user_arr.push(result)
  }
  return user_arr;
}
export function formateGroupTree(e){//格式化小组穿梭框
  depts = e;
  return transGroupTreeData(e);
}

let roleData=[] 
function transRoleTreeData(items) {
  if (items.length > 0) {
    let curPid = 0 //pid=0,为最上层节点 ,即无父节点 
    let parent = findRoleChild(curPid); //数组 
    return parent;
  } else {
    return [];
  }
}
//找子节点 
function findRoleChild(curPid) {
  let _arr = [];
  let items = roleData;
  let length = items.length;
  for (let i = 0; i < length; i++) {
    if ( curPid == 0) {
      let _obj = items[i];
      _obj.label = items[i].role_name;
      _obj.children = findRoleChild(_obj.role_id);
      _obj.must = false ;
      user_listAll({role_id: _obj.id, company_id: localStorage.getItem("companyId")}).then(res => {
        let formateUser = fomateRoleUser(res.data.data.rows);
        _obj.children = _obj.children.concat(formateUser);
        _obj.num = formateUser.length //人数
        _arr.push(_obj);
      });
    }
  }
  return _arr;
}

function fomateRoleUser(e){
  let user_arr = []
  for (let i = 0; i < e.length; i++) {
    let user = e[i];
    let args = { label: user.user_name, id: "user_id" + user.id, must: true }
    let result = Object.assign({}, user, args)
    user_arr.push(result)
  }
  return user_arr;
}

export function formateRoleTree(e) {//格式化成树结构
  roleData = e;
    return transRoleTreeData(e);
}


let groupDepts=[] 
function GroupTreeData(items) {
  if (items.length > 0) {
    let curPid = 0 //pid=0,为最上层节点 ,即无父节点 
    let parent = GroupChild(curPid); //数组 
    return parent;
  } else {
    return [];
  }
}
//找子节点 
function GroupChild(curPid) {
  let _arr = [];
  let items = groupDepts;
  let length = items.length;
  for (let i = 0; i < length; i++) {
    if (curPid == 0) {
      let _obj = items[i];
      _obj.label = items[i].group_name;
      _obj.must = false ;
      _obj.children = GroupChild(_obj.id);
      group_findUsersById({id: _obj.id, company_id: localStorage.getItem("companyId")}).then(res => {
        let formateUser = fomateGroup(res.data.data.rows);
        _obj.children = _obj.children.concat(formateUser);
        _obj.num = formateUser.length //人数
        _arr.push(_obj);
      });
    }
  }
  return _arr;
}
function fomateGroup(e){
  let user_arr = []
  for (let i = 0; i < e.length; i++) {
    let user = e[i];
    let args = { label: user.user_name, id: "user_id" + user.id, must: true }
    let result = Object.assign({}, user, args)
    user_arr.push(result)
  }
  return user_arr;
}
export function GroupTree(e){ //格式化小组穿梭框(带人员)
  groupDepts = e;
  return GroupTreeData(e);
}